package com.my.test.web.goods;

import com.my.test.service.goods.model.Goods;
import com.my.test.service.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by vivi on 2017/12/9.
 */
@Controller
@RequestMapping(value = "/goods")
public class GoodsCtl {

    @Autowired
    private GoodsService goodsService;

    @ResponseBody
    @RequestMapping(value = "/updateGoods")
    public String updateGoods(){
        goodsService.test1();
        return "{\"res\":\"ok\"}";
    }

    @ResponseBody
    @RequestMapping(value = "/findAll.json")
    public String findAll(){
       // List<Goods> list = goodsService.findAll();
        return "{\"11\":\"22\"}";
    }

    public static void count1() {
        File csv = new File("C:\\Users\\Administrator\\Desktop\\order.csv");  // CSV文件路径
        File csv2 = new File("C:\\Users\\Administrator\\Desktop\\redeem.csv");  // CSV文件路径
        File result = new File("C:\\Users\\Administrator\\Desktop\\reslut.txt");  // 结果文件路径
        BufferedReader br = null;
        BufferedReader br2 = null;
        OutputStreamWriter writer=null;
        try
        {
            br = new BufferedReader(new FileReader(csv));
            br2 = new BufferedReader(new FileReader(csv2));

            String charSet="UTF-8";
            //写字符转换成字节流
            FileOutputStream fileWriter=new FileOutputStream(result);
            writer=new OutputStreamWriter(fileWriter, charSet);

            String line = "";
            String everyLine = "";
            String line2 = "";
            String everyLine2 = "";
            Map<String,List<String>> map = new TreeMap<>();

            List<String> allString = new ArrayList<>();
            while ((line = br.readLine()) != null)  //读取到的内容给line变量
            {
                everyLine = line;
                //System.out.println(everyLine);
                String[] arr = everyLine.split(",");
                //取一天内所有的用户
                String currDate= arr[1];
                if(map.get(currDate)== null){
                    List<String> list = new ArrayList<String>();
                    list.add(arr[0]);
                    map.put(currDate,list);
                }else{
                    List<String> list = map.get(currDate);
                    list.add(arr[0]);
                    map.put(currDate,list);
                }

                //allString.add(everyLine);
            }
            for (String key : map.keySet()) {
                //System.out.println("Key = " + key );
                List<String> uids = map.get(key);
                List<String> newIds = removeDuplicate(uids);
                //也可以用这个去重：uids = uids.stream().distinct().collect(Collectors.toList());
                map.put(key,newIds);
                //System.out.println("Key = " + key + "valus="+newIds);

            }
            //allString.stream().distinct().forEach(System.out::println);
            //System.out.println("csv表格中所有行数："+allString.size());

//------------------------------------------------------------------
            List<String> redeemString = new ArrayList<>();

            while ((line2 = br2.readLine()) != null)  //读取到的内容给line2变量
            {
                everyLine2 = line2;
                //System.out.println(everyLine2);
                redeemString.add(everyLine2);
            }

            //allString.stream().distinct().forEach(System.out::println);
            //System.out.println("csv表格中所有行数："+redeemString.size());



            //-----------------------------循环数据
            java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
            Map<String,Integer> innerMap = new HashMap<>();
            for (String key : map.keySet()) {
                int redeemCount=0;
                String currDate = key;
                Calendar cal = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。
                Date d = format.parse(currDate);
                cal.setTime(d);
                //System.out.println("today is "+ format.format(d));
                cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前一天.
                String yesterday1 = format.format(cal.getTime());
                //System.out.println("yesterday is:"+format.format(cal.getTime()));
                cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前2天.
                String yesterday2 = format.format(cal.getTime());
                //System.out.println("yesterday2 is:"+format.format(cal.getTime()));
                cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前3天.
                String yesterday3 = format.format(cal.getTime());
                //System.out.println("yesterday3 is:"+format.format(cal.getTime()));
                cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前4天.
                String yesterday4 = format.format(cal.getTime());
                //System.out.println("yesterday4 is:"+format.format(cal.getTime()));
                cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前5天.
                String yesterday5 = format.format(cal.getTime());
                //System.out.println("yesterday5 is:"+format.format(cal.getTime()));
                cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前6天.
                String yesterday6 = format.format(cal.getTime());
                //System.out.println("yesterday6 is:"+format.format(cal.getTime()));
                cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前7天.
                String yesterday7 = format.format(cal.getTime());
                //System.out.println("yesterday7 is:"+format.format(cal.getTime()));
                List<String> orderUids = map.get(key);
                for(String oid : orderUids){
                    innerMap.clear();
                    for (String redeemStr : redeemString){
                        String[] redeemArr = redeemStr.split(",");
                        String redeemUid = redeemArr[0];
                        String redeemDate = redeemArr[1];
                        if(oid.equals(redeemUid)){
                            if(redeemDate.equals(yesterday1) || redeemDate.equals(yesterday2) || redeemDate.equals(yesterday3) || redeemDate.equals(yesterday4) || redeemDate.equals(yesterday5) || redeemDate.equals(yesterday6) || redeemDate.equals(yesterday7)){
                                innerMap.put(oid,1);
                            }
                            /*if(redeemDate.equals(yesterday2)){
                                redeemCount++;
                            }
                            if(redeemDate.equals(yesterday3)){
                                redeemCount++;
                            }
                            if(redeemDate.equals(yesterday4)){
                                redeemCount++;
                            }
                            if(redeemDate.equals(yesterday5)){
                                redeemCount++;
                            }
                            if(redeemDate.equals(yesterday6)){
                                redeemCount++;
                            }
                            if(redeemDate.equals(yesterday7)){
                                redeemCount++;
                            }*/
                        }
                    }
                    redeemCount += innerMap.size();
                }
                System.out.println("date="+key+" count="+map.get(key).size()+" before7days="+redeemCount);
                writer.write("date="+key+" count="+map.get(key).size()+" before7days="+redeemCount+"\n");
                writer.flush();
            }

        } catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public   static   List  removeDuplicate(List<String> list)  {
        for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {
            for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {
                if  (list.get(j).equals(list.get(i)))  {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    public static void main(String[] args) {
        // count1();
        count2();
    }

    public static void count2() {
        for(int i=1;i<=7;i++){
            File csv = new File("C:\\Users\\Administrator\\Desktop\\count2\\order2018-0"+i+".csv");  // CSV文件路径
            File csv2 = new File("C:\\Users\\Administrator\\Desktop\\count2\\redeem2018-0"+i+".csv");  // CSV文件路径
            File result = new File("C:\\Users\\Administrator\\Desktop\\count2\\result\\reslut2018-0"+i+".txt");  // 结果文件路径
            BufferedReader br = null;
            BufferedReader br2 = null;
            OutputStreamWriter writer=null;
            try
            {
                br = new BufferedReader(new FileReader(csv));
                br2 = new BufferedReader(new FileReader(csv2));

                String charSet="UTF-8";
                //写字符转换成字节流
                FileOutputStream fileWriter=new FileOutputStream(result);
                writer=new OutputStreamWriter(fileWriter, charSet);

                String line = "";
                String everyLine = "";
                String line2 = "";
                String everyLine2 = "";
                Map<String,List<String>> map = new TreeMap<>();

                List<String> allString = new ArrayList<>();
                while ((line = br.readLine()) != null)  //读取到的内容给line变量
                {
                    everyLine = line;
                    //System.out.println(everyLine);
                    String[] arr = everyLine.split(",");
                    //取一天内所有的用户
                    String currDate= arr[1];
                    if(map.get(currDate)== null){
                        List<String> list = new ArrayList<String>();
                        list.add(arr[0]+","+arr[2]);
                        map.put(currDate,list);
                    }else{
                        List<String> list = map.get(currDate);
                        list.add(arr[0]+","+arr[2]);
                        map.put(currDate,list);
                    }

                    //allString.add(everyLine);
                }
            /*for (String key : map.keySet()) {
                List<String> uids = map.get(key);
                List<String> newIds = removeDuplicate(uids);//去重
                //也可以用这个去重：uids = uids.stream().distinct().collect(Collectors.toList());
                //System.out.println("Key = " + key + "valus="+newIds);
                map.put(key,newIds);

            }*/
                //allString.stream().distinct().forEach(System.out::println);
                //System.out.println("csv表格中所有行数："+allString.size());

//------------------------------------------------------------------
                List<String> redeemString = new ArrayList<>();

                while ((line2 = br2.readLine()) != null)  //读取到的内容给line2变量
                {
                    everyLine2 = line2;
                    //System.out.println(everyLine2);
                    redeemString.add(everyLine2);
                }

                //allString.stream().distinct().forEach(System.out::println);
                //System.out.println("csv表格中所有行数："+redeemString.size());

                //-----------------------------循环数据
                java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
                Map<String,Integer> innerMap = new HashMap<>();
                List<String> uidInbefore7Day = new ArrayList<>();//前7天内所有用户的uid
                writer.write("日期          当日订单数        前7日赎回单数        前7日赎回的用户在当日总投资/元a         所有用户当日总投资/元b          a/b比例"+"\n");
                for (String key : map.keySet()) {
                    long start = System.currentTimeMillis();
                    int redeemCount=0;
                    long buyTotalDay = 0L;
                    long allTotalDay = 0L;
                    innerMap.clear();
                    uidInbefore7Day.clear();
                    String currDate = key;
                    Calendar cal = Calendar.getInstance();//使用默认时区和语言环境获得一个日历。
                    Date d = format.parse(currDate);
                    cal.setTime(d);
                    //System.out.println("today is "+ format.format(d));
                    cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前一天.
                    String yesterday1 = format.format(cal.getTime());
                    //System.out.println("yesterday is:"+format.format(cal.getTime()));
                    cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前2天.
                    String yesterday2 = format.format(cal.getTime());
                    //System.out.println("yesterday2 is:"+format.format(cal.getTime()));
                    cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前3天.
                    String yesterday3 = format.format(cal.getTime());
                    //System.out.println("yesterday3 is:"+format.format(cal.getTime()));
                    cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前4天.
                    String yesterday4 = format.format(cal.getTime());
                    //System.out.println("yesterday4 is:"+format.format(cal.getTime()));
                    cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前5天.
                    String yesterday5 = format.format(cal.getTime());
                    //System.out.println("yesterday5 is:"+format.format(cal.getTime()));
                    cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前6天.
                    String yesterday6 = format.format(cal.getTime());
                    //System.out.println("yesterday6 is:"+format.format(cal.getTime()));
                    cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前7天.
                    String yesterday7 = format.format(cal.getTime());
                    //System.out.println("yesterday7 is:"+format.format(cal.getTime()));
                    List<String> orderUids = map.get(key);

                    for (String redeemStr : redeemString){//循环所有的的赎回单
                        String[] redeemArr = redeemStr.split(",");
                        String redeemUid = redeemArr[0];
                        String redeemDate = redeemArr[1];
                        if(redeemDate.equals(yesterday1) || redeemDate.equals(yesterday2) || redeemDate.equals(yesterday3) || redeemDate.equals(yesterday4) || redeemDate.equals(yesterday5) || redeemDate.equals(yesterday6) || redeemDate.equals(yesterday7)){
                            uidInbefore7Day.add(redeemUid);
                        }
                    }
                    uidInbefore7Day = removeDuplicate(uidInbefore7Day);//去重获得前7日内所有赎回单用户id

                    for(String oid : orderUids){
                        String[] uidbuys = oid.split(",");
                        for (String redeemuid : uidInbefore7Day){
                            if(uidbuys[0].equals(redeemuid)){
                                buyTotalDay+=Long.valueOf(uidbuys[1]);//当天的前7日内赎回的用户在今天投资
                                innerMap.put(redeemuid,1);
                            }
                        }
                        allTotalDay+=Long.valueOf(uidbuys[1]);
                    }

                    BigDecimal rate = BigDecimal.valueOf(buyTotalDay).divide(BigDecimal.valueOf(allTotalDay),4,BigDecimal.ROUND_HALF_UP);
                    System.out.println(key+"         "+map.get(key).size()+"        "+innerMap.size() + "        "+buyTotalDay/100+"         "+allTotalDay/100+"          "+rate.toString());
                    writer.write(key+"         "+map.get(key).size()+"        "+innerMap.size() + "        "+buyTotalDay/100+"         "+allTotalDay/100+"          "+rate.toString()+"\n");
                    System.out.println("耗时"+(System.currentTimeMillis()-start)/1000+"s");
                    writer.flush();
                }
            } catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
